package gson.emlog.activity;

import gson.emlog.R;
import gson.emlog.adapter.BlogsAdapter;
import gson.emlog.utils.Action;
import gson.emlog.utils.Http;
import gson.emlog.utils.Result;

import org.json.JSONArray;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;

/**
 * ��־
 * 
 * @author GodSon
 * 
 */
public class Blogs extends Activity {
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.blogs);

		final ListView lv = (ListView) findViewById(R.id.blogsView);
		
		lv.setOnItemLongClickListener(itemLongClickListener);
		Button newBtn = (Button) findViewById(R.id.blognew);
		newBtn.setOnClickListener(newBtnOnClickListener);
		
		Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			public void run() {
				Http http = new Http(Blogs.this);
				Result obj = http.post(Action.BLOG_BLOGS);
				JSONArray data = new JSONArray();
				if (obj.getStatus()) {
					data = (JSONArray) obj.getData();
				}
				BlogsAdapter sadpt = new BlogsAdapter(data, context);
				lv.setAdapter(sadpt);
			}
		};
		handler.postDelayed(runnable, 500);
	}

	private View.OnClickListener newBtnOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			startActivity(new Intent(context, Write.class));
		}
	};
	
	private AdapterView.OnItemLongClickListener itemLongClickListener = new OnItemLongClickListener() {

		public boolean onItemLongClick(AdapterView<?> adv, View v,
				int item, long arg3) {
			Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(R.string.ctrol);
			Resources rs = getResources();
			final String[] its = new String[] {
					rs.getString(R.string.delete),
					rs.getString(R.string.shield),
					rs.getString(R.string.verify) };

			alert.setItems(its, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					final AlertDialog ad = new AlertDialog.Builder(context)
							.setMessage(
									"��ѡ����ǣ�" + which + ": " + its[which])
							.show();

					Handler handler = new Handler();
					Runnable runnable = new Runnable() {
						public void run() {
							ad.dismiss();

						}
					};
					handler.postDelayed(runnable, 5 * 1000);
				}
			});
			alert.create().show();
			return false;
		}
	};
}
